﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_user_ActiveUser : System.Web.UI.Page
{
    static User userToEdit;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // hide info panel by default
            ltrInfo.Text = "hide";
        }
        else
        {
            // reset result message
            ltrResultMessage.Text = "";
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        userToEdit = UserManagement.GetUser(txtUserName.Text.Trim());
        if (userToEdit != null)
        {
            txtFullName.Text = userToEdit.Fullname;
            txtEmail.Text = userToEdit.Email;
            txtOrganization.Text = userToEdit.CustomInfo;
            chbActive.Checked = userToEdit.IsActive;

            // show info panel
            ltrInfo.Text = "";
        }
        else
        {
            ltrResultMessage.Text = "Cannot find user with username \"" + txtUserName.Text + "\". Please try again!";
            // hide info panel
            ltrInfo.Text = "hide";
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (chbActive.Checked != userToEdit.IsActive)
        {
            if (Master.Master.CurrentUserManagement.UpdateUser(txtUserName.Text.Trim(), chbActive.Checked))
                ltrResultMessage.Text = "New User Status Set Successfully!";
            else
                ltrResultMessage.Text = "Fail to set new Status. Possibly due to Database Error!";
        }
        else
            ltrResultMessage.Text = "New status is the same as old one, no need to update!";
    }
}